#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define high 20
#define width 50
#define enemyNumber 5//敌机数目
//全局变量
int canvas[high][width] = {0};//二维数组记录游戏画面中对应的元素
//0输出空格，1输出飞机，2输出子弹，3输出敌机
int position_x, position_y;//我方飞机坐标
int enemy_x[enemyNumber], enemy_y[enemyNumber];//敌机坐标
int score;//分数
int bulletwidth;//子弹宽度
//光标移动到x,y位置
void gotoxy(int x, int y) {
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
//第二个值为0表示隐藏光标
void HideCursur() {
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//数据初始化
void startup() {
	bulletwidth = 5;
	int k;
	for (k = 0; k < enemyNumber; k++) {
		enemy_x[k] = rand()%3;
		enemy_y[k] = rand()%width;
		canvas[enemy_x[k]][enemy_y[k]] = 3;
	}
	score = 0;
	position_x = high / 2;
	position_y = width / 2;
	canvas[position_x][position_y] = 1;
	HideCursur();//隐藏光标
}
//显示画面
void show() {
	gotoxy(0, 0);//光标移动到原点位置，以下重画清屏
	int i, j;
	for (i = 0; i < high - 1; i++) {
		for (j = 0; j < width - 1; j++) {
			if (canvas[i][j] == 1) {
				printf("*");//输出飞机*
			}
			if (canvas[i][j] == 2) {
				printf("|");//输出子弹|
			}
			if (canvas[i][j] == 3) {
				printf("@");//输出敌机@
			}
			else {
				printf(" ");
			}
		}
		printf("\n");
	}
	printf("score:%d", score);
}
//与用户输入无关的更新
void updatewithoutinput() {
	int i, j , k;
	for (i = 0; i < high - 1; i++) {
		for (j = 0; j < width - 1; j++) {
			if (canvas[i][j] == 2) {
				for (k = 0; k < enemyNumber; k++) {
					if (i == enemy_x[k] && j == enemy_y[k]) {//击中敌机
						canvas[enemy_x[k]][enemy_y[k]] = 0;
						enemy_x[k] = 0;
						enemy_y[k] = rand() % width;
						canvas[enemy_x[k]][enemy_y[k]] = 3;
						score++;
					}
				}
				//让子弹自动向上移动
				canvas[i][j] = 0;
				if (i > 0) {
					canvas[i - 1][j] = 2;
				}
			}
		}
	}
	for (k = 0; k < enemyNumber; k++) {
		if (enemy_x[k] > high) {//敌机跑出下边界
			canvas[enemy_x[k]][enemy_y[k]] = 0;
			enemy_x[k] = 0;
			enemy_y[k] = rand() % width;
			canvas[enemy_x[k]][enemy_y[k]] = 3;
			score--;
		}
	}
	static int speed = 10;
	if (speed < 10) {
		speed++;
	}
	if (speed == 10) {
		for (k = 0; k < enemyNumber; k++) {
			//敌机自动下落
			canvas[enemy_x[k]][enemy_y[k]] = 0;
			enemy_x[k]++;
			canvas[enemy_x[k]][enemy_y[k]] = 3;
		}
		speed = 0;
	}
}
//与用户输入有关的更新
void updatewithinput() {
	char input;
	if (_kbhit()) {
		input = _getch();
		if (input == 'w') {
			canvas[position_x][position_y] = 0;
			position_x--;
			canvas[position_x][position_y] = 1;
		}
		if (input == 'a') {
			canvas[position_x][position_y] = 0;
			position_y--;
			canvas[position_x][position_y] = 1;
		}
		if (input == 's') {
			canvas[position_x][position_y] = 0;
			position_x++;
			canvas[position_x][position_y] = 1;
		}
		if (input == 'd') {
			canvas[position_x][position_y] = 0;
			position_y++;
			canvas[position_x][position_y] = 1;
		}
		if (input == ' ') {
			int left, right;
			int x;
			left = position_y - bulletwidth;
			if (left < 0) {
				left = 0;
			}
			right = position_y + bulletwidth;
			if (right > width - 1) {
				right = width - 1;
			}
			for (x = left; x < right; x++) {
				canvas[position_x - 1][x] = 2;
			}
		}
	}
}
int main() {
	startup();
	while (1) {
		show();
		updatewithoutinput();
		updatewithinput();
	}
	return 0;
}